class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length <= 1) {
            return 0;
        }
        int min = prices[0], max = 0;
        for (int i = 1; i < prices.length; i++) {
            int diff = prices[i] - min;
            max = diff > max ? diff : max;
            min = prices[i] < min ? prices[i] : min;
        }
        return max;
    }
}